package com.key.base.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.key.base.service.IBaseService;
import com.key.base.service.serviceImp.BaseServiceImp;
import com.key.common.model.Page;
import com.key.common.service.PagingFactory;
import com.key.util.XspMessage;
import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport {

	IBaseService baseService;

	public IBaseService getBaseService() {
		return new BaseServiceImp();
	}

	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}

	private String[] List_Selected;

	private static ApplicationContext ctx = null;
	private Page page;

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	/**
	 * 根据spring配置的业务对象名称，取得业务对象实例。
	 * 
	 * @param name
	 *            spring配置的业务对象名称
	 * @return spring的业务对象（service）
	 */
	protected Object getBean(String name) {
		return getSpringApplicationContext().getBean(name);
	}

	protected ApplicationContext getSpringApplicationContext() {
		if (ctx == null) {
			ctx = WebApplicationContextUtils
					.getRequiredWebApplicationContext(ServletActionContext
							.getServletContext());
		}
		return ctx;
	}

	/**
	 * 增
	 * 
	 * @return
	 */
	public String add() {
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("methodStyle", "add");
		return "add";
	}

	/**
	 * 存
	 * 
	 * @return
	 */
	public abstract String save();

	/**
	 * 查看
	 * 
	 * @return
	 */
	public String view() {
		XspMessage xspMessage = new XspMessage();
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if (page == null)
				page = new Page();
			// 对象名称
			String classname = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			String id = request.getParameter("id");
			page.setObjName(classname + " t");
			page.setCondition(" id = '" + id + "' ");
			// 查询属性(为空查询所有)
			// 调用工厂获得数据
			page = getPagingFactory().queryObjList(page);
			String[] names = classname.split("[.]");
			String str = names[names.length - 1];
			str = str.substring(0, 1).toLowerCase() + str.substring(1);
			if (page.getObjList() != null && page.getObjList().size() > 0) {
				request.setAttribute(str, page.getObjList().get(0));
			} else {
				request.setAttribute(str, null);
			}
		} catch (Exception e) {
			return xspMessage
					.setMessage("查看失败", e.toString(), XspMessage.ERROR);
		}
		return "view";
	}

	/**
	 * 编辑
	 * 
	 * @return
	 */
	public String edit() {
		XspMessage xspMessage = new XspMessage();
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			request.setAttribute("methodStyle", "edit");
			if (page == null)
				page = new Page();
			// 对象名称
			String classname = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			String id = request.getParameter("id");
			page.setObjName(classname + " t");
			page.setCondition(" id = '" + id + "' ");
			// 查询属性(为空查询所有)
			// 调用工厂获得数据
			page = getPagingFactory().queryObjList(page);
			String[] names = classname.split("[.]");
			String str = names[names.length - 1];
			str = str.substring(0, 1).toLowerCase() + str.substring(1);
			if (page.getObjList() != null && page.getObjList().size() > 0) {
				request.setAttribute(str, page.getObjList().get(0));
			} else {
				request.setAttribute(str, null);
			}
		} catch (Exception e) {
			return xspMessage
					.setMessage("编辑失败", e.toString(), XspMessage.ERROR);
		}
		return "edit";
	}

	/**
	 * 删
	 * 
	 * @return
	 */
	public String delete() {
		return deleteAll();
	}

	/**
	 * 删
	 * 
	 * @return
	 */
	public String deleteAll() {
		// TODO Auto-generated method stub
		XspMessage xspMessage = new XspMessage();
		try {
			// 对象名称
			String className = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			getBaseService().deleteAllObj(className, getList_Selected());
		} catch (Exception e) {
			return xspMessage
					.setMessage("删除失败", e.toString(), XspMessage.ERROR);
		}
		return "Success";
	}

	/**
	 * 改
	 * 
	 * @return
	 */
	public abstract String update();

	public PagingFactory getPagingFactory() {
		PagingFactory pf = (PagingFactory) getBean("pageFactory");
		return pf;
	}

	/**
	 * 列表
	 * 
	 * @return
	 */
	public String list() {
		XspMessage xspMessage = new XspMessage();
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			if (page == null)
				page = new Page();
			// 对象名称
			String classname = getClass().getName().replace(".action.",
					".model.").replace("Action", "");
			page.setObjName(classname + " t");
			// 每页显示条数（默认10）
			page.setPageSize(15);
			// 查询属性(为空查询所有)
			// 调用工厂获得数据
			page = getPagingFactory().queryObjList(page);
			request.setAttribute("page", page);
		} catch (Exception e) {
			return xspMessage.setMessage("查看列表失败", e.toString(),
					XspMessage.ERROR);
		}
		return "list";
	}

	public String[] getList_Selected() {
		return List_Selected;
	}

	public void setList_Selected(String[] list_Selected) {
		List_Selected = list_Selected;
	}
}
